# coding: utf-8

import requests

from common import pytest_config, logger


class RequestsUtil:
    session = requests.session()

    @classmethod
    def send_request(cls, url, method, headers=None, **kwargs):
        """
        使用指定的方法向指定的URL发送请求。
        Send a request to the specified URL with the specified method.
        :param url: 请求地址
        :param method: 请求方法
        :param headers: 请求头
        :param kwargs: 请求参数
        :return:
        """
        kwargs['timeout'] = kwargs.pop('timeout', pytest_config.request_setting.timeout)
        kwargs['json'] = kwargs.pop('json_', None)
        if headers is None:
            headers = {}
        if 'User-Agent' not in headers:
            headers.update({'User-Agent': pytest_config.request_setting.user_agent})
        logger.info(f'请求方式：{method}')
        logger.info(f'请求地址：{url}')
        logger.debug(f'请求头：{headers}')
        logger.info(f'请求参数：{kwargs}')
        response = requests.session().request(url=url, method=method, headers=headers, **kwargs)
        logger.debug(f'响应状态码：{response.status_code}')
        logger.debug(f'响应内容：{response.text}')
        if response.status_code != 200:
            logger.error(f'响应内容：{response.text}')

        return response
